package com.app.milkteaapp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.app.milkteaapp.bean.MilkTeaGsonBean;
import com.app.milkteaapp.R;
import com.app.milkteaapp.utils.MyTools;
import com.app.milkteaapp.fragment.MilkteaFragment;
import com.squareup.picasso.Picasso;

import java.util.List;

public class MilkTeaAdapter extends BaseAdapter {

    private List<MilkTeaGsonBean.MilkTea> objects;
    private MilkteaFragment milkteaFragment;
    private Context context;
    private LayoutInflater layoutInflater;

    public MilkTeaAdapter(Context context, List<MilkTeaGsonBean.MilkTea> list, MilkteaFragment milkteaFragment) {
        this.milkteaFragment = milkteaFragment;
        this.objects = list;
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public MilkTeaGsonBean.MilkTea getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.list_item_milktea, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews((MilkTeaGsonBean.MilkTea) getItem(position), (ViewHolder) convertView.getTag());
        return convertView;
    }

    private void initializeViews(final MilkTeaGsonBean.MilkTea milkTea, ViewHolder holder) {
        //加载图片
        Picasso.get()
                .load(MyTools.URL + "/" + milkTea.getImg())
                .into(holder.imgMilkTeaList);
        holder.tvMilkTeaName.setText(milkTea.getName());
        holder.tvMilkTeaSales.setText(milkTea.getSales()+"");
        holder.tvMilkTeaPrice.setText(milkTea.getPrice()+"");
        holder.btnAddCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                milkteaFragment.addOrderAndAddToCart(milkTea.getId());
            }
        });
    }

    protected class ViewHolder {
        ImageView imgMilkTeaList;
        TextView tvMilkTeaName;
        TextView tvMilkTeaPrice;
        TextView tvMilkTeaSales;
        Button btnAddCart;

        public ViewHolder(View view) {
            imgMilkTeaList = view.findViewById(R.id.img_milk_list);
            tvMilkTeaName = view.findViewById(R.id.tv_milktealist_name);
            tvMilkTeaPrice = view.findViewById(R.id.tv_milktealist_price);
            tvMilkTeaSales = view.findViewById(R.id.tv_milktealist_sales);
            btnAddCart = view.findViewById(R.id.btn_add_cart);
        }
    }

}
